const app = getApp()
import { editUserInfo } from '../../utils/requestApi.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    showSheet: false, //显示隐藏 性别弹框
    actions:[ { name: '女' }, { name: '男' } ], //性别数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let { userInfo } = this.data;
    wx.getStorage({
      key: 'userInfo',
      success: (res) =>{
        userInfo = {
          img: res.data.picture,
          username: res.data.NickName ? res.data.NickName : res.data.UserName,
          username2: res.data.realName,
          phone: res.data.CellPhone,
          sex: res.data.Gender == 0 ? '' : (res.data.Gender == 1 ? '男' :'女'),
          birthday: res.data.BirthDate ? res.data.BirthDate.split('T')[0] : '',
          qq: res.data.QQ,
          address: res.data.Address,
          isSignToday: res.data.IsSignToday,
        }
        this.setData({ userInfo })
      },
    })
  },

  /**
   * 获取微信头像
   * */
  getWxPic: function(e) {
    wx.showLoading({ title: '获取中...' })
    let { userInfo } = this.data;
    wx.getUserInfo({
      success: (res) => {
        wx.hideLoading();
        userInfo.img = res.userInfo.avatarUrl;
        this.setData({ userInfo })
      }
    })
  },

  // 重新上传图片
  imgLoad: function() {
    let { userInfo } = this.data;
    wx.chooseImage({
      count: 1,
      sourceType: ['album', 'camera'],
      success: (res) => {
        wx.showLoading({ title: '上传图片中...' })
        wx.uploadFile({
          url: app.data.url + '/API/ImageHandler.ashx?action=NewUpload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: { Cookie: app.data.cookie },
          success: (res) => {
            wx.hideLoading();
            userInfo.img = JSON.parse(res.data).Data.ImageURL;
            this.setData({ userInfo })
          }
        })
      }
    })
  },
   
  // 选择值
  changeInput(e){
    console.log(e)
    let { type } = e.currentTarget.dataset;
    let { userInfo } = this.data;
    if (type == 0) userInfo.username = e.detail.value;
    if (type == 1) userInfo.username2 = e.detail.value;
    if (type == 2) {
      userInfo.sex = e.detail.name;
      this.setData({ showSheet: !this.data.showSheet, userInfo })
    }
    if (type == 3) userInfo.birthday = e.detail.value
    if (type == 4) userInfo.phone = e.detail.value
    if (type == 5) userInfo.qq = e.detail.value
    if (type == 6) userInfo.address = e.detail.value
    this.setData({ userInfo })
  },

  // 表单提交  正则匹配之后再调用保存接口
  formSubmit: function (e) {
    let { username, username2, sex, birthday, phone, qq, address } = e.detail.value;
    if (!username) {
      wx.showToast({ icon: 'none', title: '请输入昵称' })
      return
    }
    if (!username2) {
      wx.showToast({ icon: 'none', title: '请输入姓名' })
      return
    }
    if (!sex) {
      wx.showToast({ icon: 'none', title: '请选择性别' })
      return
    }
    if (!birthday) {
      wx.showToast({ icon: 'none', title: '请选择日期' })
      return
    }
    if (!/^1[3|4|5|7|8][0-9]{9}$/.test(phone)) {
      wx.showToast({ icon: 'none', title: '请输入正确的手机号码' })
      return
    }
    if (!qq) {
      wx.showToast({ icon: 'none', title: '请输入QQ号码' })
      return
    }
    if (!address) {
      wx.showToast({ icon: 'none', title: '请选择地址' })
      return
    }
    editUserInfo({
      gender: sex == '男'? 1:2,
      birthday: birthday,
      qq: qq,
      picture: this.data.userInfo.img,
      address: address,
      nickName: username,
      realName: username2,
      cellPhone: phone
    }).then(res =>{
      if(res.data.Status == 'Success') {
        wx.showToast({ title: '修改成功' })
        setTimeout(() =>{
          wx.navigateBack({ delta:1 })
        },1000)
      } else wx.showToast({ icon: 'none', title: '修改失败' })
    })
  }

})